package tools.data;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamByteStream {
    private InputStream is;
    private long read;

    public InputStreamByteStream(InputStream is) {
        this.read = 0L;
        this.is = is;
    }

    public int readByte() {
        try {
            int temp = this.is.read();
            if (temp == -1) {
                throw new RuntimeException("EOF");
            }
            ++this.read;
            return temp;
        } catch (IOException e) {
            throw new RuntimeException((Throwable) e);
        }
    }

    public long getBytesRead() {
        return this.read;
    }

    public long available() {
        try {
            return (long) this.is.available();
        } catch (IOException e) {
            System.err.println("ERROR" + e);
            return 0L;
        }
    }

    public String toString(boolean b) {
        return this.toString();
    }

    public byte[] toByteArray() throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] data = new byte[16384];
        int nRead;
        while ((nRead = this.is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();
        return buffer.toByteArray();
    }
}
